﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Drawing.Imaging;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace SMSTasker
{
	public static class ControlBitmapExtensions
	{
	    [DllImport("coredll.dll")]
	    public static extern bool BitBlt(IntPtr hdc, int nXDest, int nYDest, int nWidth, int nHeight,
	        IntPtr hdcSrc, int nXSrc, int nYSrc, int dwRop);

        [DllImport("coredll.dll", EntryPoint = "SetBkMode", SetLastError = true)]
        public static extern int SetBkModeCE(IntPtr hdc, BkMode iBkMode);

        public enum BkMode { 
            TRANSPARENT=1,
            OPAQUE=2
        }
	
	    public struct PixelData
	    {
	        public byte Blue;
	        public byte Green;
	        public byte Red;
	    }
	
	    public static Bitmap GetSnapshot(Control Control)
	    {
	        Rectangle rect = new Rectangle(0, 0, Control.Width, Control.Height - 1);
	        Graphics g = Control.CreateGraphics();
	        Bitmap Snapshot = new Bitmap(rect.Width, rect.Height);
	        Graphics gShapshot = Graphics.FromImage(Snapshot);
            BitBlt(gShapshot.GetHdc(), 0, 0, rect.Width, rect.Height, g.GetHdc(), rect.Left, rect.Top, (int)TernaryRasterOperations.SRCCOPY);
	        gShapshot.Dispose();
	
	        return Snapshot;
	    }
	
	    public static unsafe Bitmap AdjustBrightness(Bitmap Bitmap, decimal Percent)
	    {
	        Percent /= 100;
	        Bitmap Snapshot = (Bitmap)Bitmap.Clone();
	        Rectangle rect = new Rectangle(0, 0, Bitmap.Width, Bitmap.Height);
	
	        BitmapData BitmapBase = Snapshot.LockBits(rect, ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
	        byte* BitmapBaseByte = (byte*)BitmapBase.Scan0.ToPointer();
	
	        // the number of bytes in each row of a bitmap is allocated (internally) to be equally divisible by 4
	        int RowByteWidth = rect.Width * 3;
	        if (RowByteWidth % 4 != 0)
	        {
	            RowByteWidth += (4 - (RowByteWidth % 4));
	        }
	
	        for (int i = 0; i < RowByteWidth * rect.Height; i += 3)
	        {
	            PixelData* p = (PixelData*)(BitmapBaseByte + i);
	
	            p->Red = (byte)Math.Round(Math.Min(p->Red * Percent, (decimal)255));
	            p->Green = (byte)Math.Round(Math.Min(p->Green * Percent, (decimal)255));
	            p->Blue = (byte)Math.Round(Math.Min(p->Blue * Percent, (decimal)255));
	        }
	
	        Snapshot.UnlockBits(BitmapBase);
	        return Snapshot;
	    }
	
	    public static Bitmap Brighten(Bitmap Bitmap, decimal PercentChange)
	    {
	        return AdjustBrightness(Bitmap, 100 + PercentChange);
	    }
	
	    public static Bitmap Darken(Bitmap Bitmap, decimal PercentChange)
	    {
	        return AdjustBrightness(Bitmap, 100 - PercentChange);
	    }

        public enum TernaryRasterOperations
        {
            SRCCOPY = 0x00CC0020, /* dest = source*/
            SRCPAINT = 0x00EE0086, /* dest = source OR dest*/
            SRCAND = 0x008800C6, /* dest = source AND dest*/
            SRCINVERT = 0x00660046, /* dest = source XOR dest*/
            SRCERASE = 0x00440328, /* dest = source AND (NOT dest )*/
            NOTSRCCOPY = 0x00330008, /* dest = (NOT source)*/
            NOTSRCERASE = 0x001100A6, /* dest = (NOT src) AND (NOT dest) */
            MERGECOPY = 0x00C000CA, /* dest = (source AND pattern)*/
            MERGEPAINT = 0x00BB0226, /* dest = (NOT source) OR dest*/
            PATCOPY = 0x00F00021, /* dest = pattern*/
            PATPAINT = 0x00FB0A09, /* dest = DPSnoo*/
            PATINVERT = 0x005A0049, /* dest = pattern XOR dest*/
            DSTINVERT = 0x00550009, /* dest = (NOT dest)*/
            BLACKNESS = 0x00000042, /* dest = BLACK*/
            WHITENESS = 0x00FF0062, /* dest = WHITE*/
        };

        public static void DrawLabel(Control ctrl, Graphics g)
        {
            g.DrawString(ctrl.Text, ctrl.Font, new SolidBrush(ctrl.ForeColor), new RectangleF(ctrl.Left, ctrl.Top, ctrl.Width, ctrl.Height)); 
        }
	}
}